package fr.norte.livraisondifferentielle.resource;

import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.ini4j.Ini;
import org.ini4j.InvalidIniFormatException;
/**
 *
 * @author Christopher Norte
 */
public class Resource {
	private static Resource instance;
	private Ini config;
	private Logger logger;
	

	private Resource() {
		this.initConfig();
		this.initLog();
	}

	public static Resource getInstance() {
		if(instance == null)
			instance = new Resource();
		return instance;
	}
	
	private void initConfig()
	{
		try {
			this.config = new Ini(new FileReader("configuration.ini"));
		} catch (InvalidIniFormatException ex) {
			System.err.println(ex);
		} catch (IOException ex) {
			System.err.println(ex);
		}
	}
	
	private void initLog()
	{
		Logger.getRootLogger().addAppender(new ConsoleAppender());

		logger = Logger.getLogger("fr.norte.livraison");
		logger.setAdditivity(false);

		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		try {
			logger.addAppender(new FileAppender(new PatternLayout("%-5p datetime : %d application:LivraisonDifferentielle location : %F:%L message : %m%n"), this.config.get("log").get("path") + "/" + dateFormat.format(new Date()) + "_livraison.log"));
		} catch (IOException ex) {
			System.out.println("Problème d'initialisation des logs : " + ex);
		}

		logger.setLevel(Level.INFO);
	}

	public Ini getConfig() {
		return config;
	}

	public Logger getLog() {
		return logger;
	}
}
